package com.dz.lm.web.controller.newuser.taklcomment;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.talkcomment.TalkComment;
import com.dz.lm.service.talkcomment.TalkComenntService;
import com.dz.lm.web.controller.BasicControl;
import com.dz.lm.web.controller.pay.WxPayControl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description; 用户评论
 * @Date: Created in 2019/12/28 14:51;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/tailComment", produces = "application/json")
public class TalkCommentController extends BasicControl {
    public Logger logger = LogManager.getLogger(WxPayControl.class);
    @Autowired
    private TalkComenntService talkComenntService;

    /**
     * 添加评论
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        Integer topicId = param.get("topicId") != null ?Integer.parseInt(param.get("topicId").toString()) : 0;
        //类型 1-用户首页2-产品详情页
        Integer commentType = param.get("commentType") != null ? Integer.parseInt(param.get("commentType").toString()) : 1;
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 0;
        Integer uid = param.get("uid") != null ? Integer.parseInt(param.get("uid").toString()) : 0;
        Integer parentId = param.get("parentId") != null ? Integer.parseInt(param.get("parentId").toString()) : 0;
        String content = param.get("content") != null ? param.get("content").toString() : "";

        if (topicId == 0 || uid == 0 || type == 0 || StringUtils.isEmpty(content)) {
            message="参数有误";
            result= "02";
        }else {
            TalkComment comment = new TalkComment();
            comment.setType(type);
            comment.setUserId(uid);
            comment.setTopicId(topicId);
            comment.setContent(StringUtil.getURLEncoderOld(content));
            comment.setCommentType(commentType);
            if(parentId > 0){
                comment.setParentId(parentId);
            }
            Date day = new Date();
            comment.setCreateTime(DateUtil.getTimeStr(day));
            int add = 0;
            add = talkComenntService.add(comment);
            if (add > 0) {
                message="添加评论成功";
                result= "00";
            }
        }

        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }

    /**
     * 删除评论
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
     //   inparam.add("mobilePhone");
       // inparam.add("password");
//		inparam.add("pushToken");
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        Integer commentId = param.get("commentId") != null ? Integer.parseInt(param.get("commentId").toString()) : 0;

        if(commentId == 0){
            message="参数有误";
            result= "02";
        }else {
            int ret = 0;
            ret = talkComenntService.delte(commentId);
            if(ret > 0 ){
                message="删除评论成功";
                result= "00";
            }
        }
        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }

    /**
     * 获取评论列表
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";

        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
       // inparam.add("mobilePhone");
        //inparam.add("password");
		//inparam.add("pushToken");
       for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        Integer topicId = param.get("topicId") != null ? Integer.parseInt(param.get("topicId").toString()) : 0;
        //类型 1-用户首页2-产品详情页
        Integer commentType = param.get("commentType") != null ? Integer.parseInt(param.get("commentType").toString()) : 1;
        Integer page = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        if(topicId == 0){
            message="参数有误";
            result= "02";
        }else {
            Map map =new HashMap();
            map.put("topicId",topicId);
            map.put("commentType",commentType);
            data  = talkComenntService.queryList(map, page, rows);
            message="获取成功";
            result= "00";
        }

        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }
}